﻿using System.Collections;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using GalaSoft.MvvmLight.Command;
using SellAvi.Annotations;
using SellAvi.Model;

namespace SellAvi.Views
{
    /// <summary>
    /// Interaction logic for ControlRegionCombobox.xaml
    /// </summary>
    public partial class ControlRegionCombobox : UserControl, INotifyPropertyChanged
    {
        public static RelayCommand FilterCitiesCommand { get; set; }
        public ControlRegionCombobox()
        {
            InitializeComponent();
            //this.DataContext = this;
            FilterCitiesCommand = new RelayCommand(() =>
            {
                OnPropertyChanged("CityList");
            });

        }

        public static readonly DependencyProperty SelectedRegionProperty =
    DependencyProperty.Register("SelectedRegion", typeof(TRegion), typeof(ControlRegionCombobox), null);

        public TRegion SelectedRegion
        {
            get { return (TRegion)GetValue(SelectedRegionProperty); }
            set { SetValue(SelectedRegionProperty, value); }
        }


        public static readonly DependencyProperty SelectedCityProperty =
            DependencyProperty.Register("SelectedCity", typeof(TRegion), typeof(ControlRegionCombobox), null);

        public TRegion SelectedCity
        {
            get { return (TRegion)GetValue(SelectedCityProperty); }
            set { SetValue(SelectedCityProperty, value); }
        }


        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(ControlRegionCombobox), null);


        public IEnumerable ItemsSource
        {
            get
            {
                return (IEnumerable)GetValue(ItemsSourceProperty);
            }
            set
            {
                SetValue(ItemsSourceProperty, value);
            }
        }

        public IEnumerable RegionList
        {
            get
            {
                var rl = ItemsSource.Cast<TRegion>().Where(x => x.ParentRegionId == null);
                return rl;

            }

        }
        public IEnumerable CityList
        {
            get
            {
                if (SelectedRegion != null)
                {
                    var cl = ItemsSource.Cast<TRegion>().Where(x => x.ParentRegionId == SelectedRegion.Id);
                    return cl;
                }
                return null;

            }
        }




        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
